package poidemo.docx4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class DocToDocx {

	public static void main(String[] args)
			throws Exception {
//		HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
//		WordprocessingMLPackage docx = Docx4J.load(new File("D:\\tmp\\docx\\test2.docx"));
//		htmlSettings.setWmlPackage(docx);
//		System.out.println("转为html");
//		htmlSettings.setImageDirPath("D:\\tmp\\docx\\images2");
//		htmlSettings.setImageTargetUri("images2");
//		Docx4J.toHTML(htmlSettings, new FileOutputStream("D:\\tmp\\docx\\test2.html"), 0);
		System.out.println("转为pdf");
//		String docx = "D:\\tmp\\docx\\D:\\tmp\\docx\\虚拟机无盘启动使用手册.doc";
		String docx = "D:\\downloads\\0518e0e9f2fdf6983cec70ccc7334f22.docx";
		WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docx));

		Mapper fontMapper = new IdentityPlusMapper();
		fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
		fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
		fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
		fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
		fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
		fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
		fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
		fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
		fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
		fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
		fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
		fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
		fontMapper.put("等线", PhysicalFonts.get("SimSun"));
		fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
		fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
		fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
		fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
		fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
		fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
		fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
		fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
		fontMapper.put("宋体扩展",PhysicalFonts.get("simsun-extB"));
		fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));
		fontMapper.put("新細明體",PhysicalFonts.get("SimSun"));
		//解决宋体（正文）和宋体（标题）的乱码问题
		PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
		PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));
		mlPackage.setFontMapper(fontMapper);

		FOSettings foSettings = Docx4J.createFOSettings();
		foSettings.setWmlPackage(mlPackage);
		Docx4J.toFO(foSettings, new FileOutputStream("D:\\tmp\\docx\\test222.pdf"), Docx4J.FLAG_EXPORT_PREFER_XSL);


//		Docx4J.toPDF(docx, new FileOutputStream("D:\\tmp\\docx\\test2.pdf"));
	}

}
